package com.sap.sap_mangers.service.Impl;

import com.sap.sap_mangers.Vo.ActivityLlkVo.ActivityLlkVo;
import com.sap.sap_mangers.entity.activityLlk.UserBestScoreRecord;
import com.sap.sap_mangers.mapper.ActivityLlkMapper;
import com.sap.sap_mangers.mapper.UserMapper;
import com.sap.sap_mangers.pojo.ActivityLlk;
import com.sap.sap_mangers.service.ActivityLlkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhouKai
 * 2022/8/2 15:32
 */
@Service
public class ActivityLlkServiceImpl implements ActivityLlkService {
    @Autowired
    private ActivityLlkMapper activityLlkMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void submitRecord(ActivityLlk activityLlk) {
        activityLlkMapper.addActivityLlkRecord(activityLlk);
    }

    @Override
    public List<ActivityLlk> getOwnRecord(String userId) {
        return activityLlkMapper.getOwnActivityLlkRecord(userId);
    }

    @Override
    public List<ActivityLlkVo> getRecordRank() {
        List<UserBestScoreRecord> bestScoreRecords = activityLlkMapper.getActivityLlkAllUserBestScoreRecord();
        bestScoreRecords.sort((o1, o2) -> o2.getScore() - o1.getScore());
        List<ActivityLlkVo> res = new ArrayList<>();
        //排名
        int rank = 1;
        for (UserBestScoreRecord userBestScoreRecord : bestScoreRecords) {
            ActivityLlk llkRecord = activityLlkMapper.getUserBestActivityLlkRecord(userBestScoreRecord);
            String realName = userMapper.getUserMessage(userBestScoreRecord.getUid()).getRealName();
            ActivityLlkVo activityLlkVo = new ActivityLlkVo(rank++, realName, llkRecord);
            res.add(activityLlkVo);
        }
        return res;
    }


}
